package com.tcp2.frame;

/**
 * Author: <黄书根>
 * Time: 2017-08-08 19:32
 * Copyright (C) 2017 Xiamen Yaxon Networks CO.,LTD.
 */


import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 界面设计
 */

public class MainFrame extends JFrame {

    private static final long serialVersionUID = 1L; // 序列化版本
   // private LoadFile loadFile=new LoadFile();
    public static boolean versionState=false;


    public MainFrame() throws Exception{

        JPanel contentPane = new JPanel();
        setContentPane(contentPane);
        setTitle("模拟终端");
        setSize(800, 640);
        setLayout(new BorderLayout());
        setLocationRelativeTo(null); // 初始在屏幕中央
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 使点击红X可以退出程序

        //程序关闭时，执行处理
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                super.windowClosing(e);
                try {
                  //  MyQuene.itMap();   //保存注册车辆的鉴权码到文件中
                  //  MyQuene.delSim();    //删除保存在文件的被注销车辆的鉴权码
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        //读取配置文件信息
      //  ServerConfig serverConfig=loadFile.loadServerConfig("./conf/app.properties");

        // 菜单栏
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu menu = new JMenu("系统");
        menuBar.add(menu);
        final JMenuItem createItem = new JMenuItem("创建车辆");
        menu.add(createItem);
     /*   if(serverConfig.getDataSource()==0) {
            CVehicleAction cv = new CVehicleAction();
            createItem.addActionListener(cv);
        }else{
            CreateVehAction cv2=new CreateVehAction();
            createItem.addActionListener(cv2);

        }*/
        JMenuItem exitItem = new JMenuItem("退出");
        menu.add(exitItem);
        exitItem.addActionListener(new ActionListener() { // 菜单中的退出按钮
            public void actionPerformed(ActionEvent event) {
                System.exit(0);
            }
        });
        JMenu menu1=new JMenu("版本");
        menuBar.add(menu1);
        JMenuItem platItem=new JMenuItem("808版..");
        menu1.add(platItem);
        JMenuItem taxiItem=new JMenuItem("905版..");
        menu1.add(taxiItem);
        JMenu simuMenu=new JMenu("模拟");
        menuBar.add(simuMenu);
        final JMenuItem customItem=new JMenuItem("自定义协议");
        simuMenu.add(customItem);
     //   CustomProAction customProAction=new CustomProAction();
       // customItem.addActionListener(customProAction);

        final JPanel jp = new JPanel();
        final CardLayout card=new CardLayout();
        jp.setLayout(card);
        TitledBorder t = new TitledBorder("");
        jp.setBorder(t);
        new PlatFrame(jp);
        new TaxiFrame(jp);
/*        JPanel jp4=(new LogFrame()).logFrame();
        byte version=serverConfig.getTermVersion();
        GpsInfo.lat=serverConfig.getLat();
        GpsInfo.lon=serverConfig.getLon();
        GpsInfo.speed=serverConfig.getSpeed();
        switch(version) {
            case 0: card.show(jp, "plat");
                setTitle("模拟终端(808版)");
                this.add(jp, "Center");
                MainFrame.versionState=false;
                this.add(jp4,"South");
                this.pack();
                this.setVisible(true);break;
            case 1: createItem.setEnabled(false);
                customItem.setEnabled(false);
                card.show(jp, "taxi");
                setTitle("模拟终端(905版)");
                this.add(jp, "Center");
                MainFrame.versionState=true;
                this.add(jp4,"South");
                this.pack();
                this.setVisible(true);break;
            default:break;
        }*/
        platItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    createItem.setEnabled(true);
                    customItem.setEnabled(true);
                    card.show(jp,"plat"); MainFrame.this.setTitle("模拟终端(808版)");MainFrame.versionState=false;
                    MainFrame.this.add(jp, "Center");MainFrame.this.pack();MainFrame.this.setVisible(true);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        taxiItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    createItem.setEnabled(false);
                    customItem.setEnabled(false);
                    card.show(jp,"taxi");
                    MainFrame.this.setTitle("模拟终端(905版)");MainFrame.versionState=true;
                    MainFrame.this.add(jp, "Center");MainFrame.this.pack();MainFrame.this.setVisible(true);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        this.pack();
    }
}
